#!/bin/bash

# Slackware build script for tetrinet

# Written by B. Watson (yalhcru@gmail.com)

# Licensed under the WTFPL. See http://www.wtfpl.net/txt/copying/ for details.

# I started writing this SlackBuid in 2020 and just now remembered to
# finish it. It's the same version packaged by Debian, so it should
# interoperate with servers running Debian.

cd $(dirname $0) ; CWD=$(pwd)

PRGNAM=tetrinet
VERSION=${VERSION:-0.11+cvs20070911}
BUILD=${BUILD:-1}
TAG=${TAG:-_SBo}
PKGTYPE=${PKGTYPE:-tgz}

if [ -z "$ARCH" ]; then
  case "$( uname -m )" in
    i?86) ARCH=i586 ;;
    arm*) ARCH=arm ;;
       *) ARCH=$( uname -m ) ;;
  esac
fi

if [ ! -z "${PRINT_PACKAGE_NAME}" ]; then
  echo "$PRGNAM-$VERSION-$ARCH-$BUILD$TAG.$PKGTYPE"
  exit 0
fi

TMP=${TMP:-/tmp/SBo}
PKG=$TMP/package-$PRGNAM
OUTPUT=${OUTPUT:-/tmp}

if [ "$ARCH" = "i586" ]; then
  SLKCFLAGS="-O2 -march=i586 -mtune=i686"
  LIBDIRSUFFIX=""
elif [ "$ARCH" = "i686" ]; then
  SLKCFLAGS="-O2 -march=i686 -mtune=i686"
  LIBDIRSUFFIX=""
elif [ "$ARCH" = "x86_64" ]; then
  SLKCFLAGS="-O2 -fPIC"
  LIBDIRSUFFIX="64"
else
  SLKCFLAGS="-O2"
  LIBDIRSUFFIX=""
fi

set -e

rm -rf $PKG
mkdir -p $TMP $PKG $OUTPUT
cd $TMP
rm -rf $PRGNAM
tar xvf $CWD/${PRGNAM}_${VERSION^^}.orig.tar.gz
cd $PRGNAM
chown -R root:root .
find -L .  -perm /111 -a \! -perm 755 -a -exec chmod 755 {} \+ -o \
        \! -perm /111 -a \! -perm 644 -a -exec chmod 644 {} \+

# 20230213 bkw: patch came from Debian:
# http://deb.debian.org/debian/pool/main/t/tetrinet/tetrinet_0.11+CVS20070911-2.1.debian.tar.xz
patch -p1 < $CWD/patches/gcc10.diff

# 20201120 bkw: fix compiler warnings, at least the ones that look like
# they might cause real problems.
patch -p1 < $CWD/patches/compilefix.diff

# 20201120 bkw: we're not building the server into the client, so don't
# offer it as an option in the help message.
sed -i '/^"  *-server/d' $PRGNAM.c

# 20201121 bkw: since we can't seem to get all the various terminals
# and ncurses to agree on the escape sequences for F1/F2/F3, allow
# switching windows with the Tab key. Grr.
patch -p1 < $CWD/patches/tab_key.diff

# 20201121 bkw: allow vi HJKL movement along with arrow keys. Also
# make the existing letter keystrokes case-insensitive.
patch -p1 < $CWD/patches/vikeys.diff

# 20201121 bkw: actually check the terminal size and abort if too
# small. This won't help if the user resizes the terminal while the
# game is running, but it's better than starting up an unplayable
# game.
patch -p1 < $CWD/patches/check_term_size.diff

# No other way to apply our flags...
sed -i "/CFLAGS/s|-g|$SLKCFLAGS|" Makefile

# *Finally*:
make

# Do not use 'make install', no DESTDIR or PREFIX/etc support.
PKGMAN6=$PKG/usr/man/man6
PKGDOC=$PKG/usr/doc/$PRGNAM-$VERSION
mkdir -p $PKG/usr/games $PKGMAN6 $PKGDOC

# Man pages come from upstream post-release CVS. I've fixed some
# typos, cleaned up the formatting, and documented all the stuff the
# patches do.
for i in $PRGNAM $PRGNAM-server; do
  install -s -m0755 $i $PKG/usr/games
  gzip -9c < $CWD/man/$i.6 > $PKGMAN6/$i.6.gz
done

# Original upstream calls the client "tetrinet", Debian calls it
# "tetrinet-client". Use symlinks to support both names.
ln -s $PRGNAM $PKG/usr/games/$PRGNAM-client
ln -s $PRGNAM.6.gz $PKG/usr/man/man6/$PRGNAM-client.6.gz

cp -a AUTHORS Change* README TODO $PRGNAM.txt $PKGDOC
cat $CWD/$PRGNAM.SlackBuild > $PKGDOC/$PRGNAM.SlackBuild

mkdir -p $PKG/install
cat $CWD/slack-desc > $PKG/install/slack-desc

cd $PKG
/sbin/makepkg -l y -c n $OUTPUT/$PRGNAM-$VERSION-$ARCH-$BUILD$TAG.$PKGTYPE
